<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['hdps']=$basic['hdps'];
if($_RQ['op']=='main'){
	set_html('title','请假记录');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$post=get_postdata();
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	//排序老师
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$return['status']=0;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=pdo_getslice('jy_kexie_body_out',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$sum=0;
	if($list){
		$sum=count($list);
		foreach($list as &$item){		
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['body']=pdo_get('jy_kexie_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['bodyid']));
			$item['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lesson_id']),'title');
			$item['classttile']=pdo_getcolumn('jy_kexie_class',array('id'=>$item['classid']),'title');
			$item['statustitle']=$this->out_type[$item['status']];
		}
	}
	$return['list']=$list;
	$return['sum']=$sum;
	set_html('title','小孩列表');
	exi($return);
}
if($_RQ['op']=='edit'){
	$bodyid=intval($_RQ['bodyid']);
	if(!$bodyid){
		exi('请选择小孩','error');
	}
	$return['body']=pdo_get('jy_kexie_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$bodyid));
	$return['class']=pdo_get('jy_kexie_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$return['body']['classid']));
	if(!$return['body']['classid']){
		exi('该小孩还未报名班级！','error');
	}
	$post=array('timeid'=>'no','storeid'=>$return['body']['storeid'],'bodyid'=>$bodyid,'classid'=>$return['body']['classid']);
	$post['dayid']=0;
	$data=$this->model('lesson')->get_days($return['body']['classid'],7);
	$return['data']=$data;
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['bodyid']){
		exi('请选择小孩','error');
	}
	if(!$postdata['lesson_id']){
		exi('请选择课程！','error');
	}
	if(!$postdata['classid']){
		exi('请选择班级！','error');
	}
	if(!$postdata['remark']){
		exi('请添加请假事由！','error');
	}
	if(!$postdata['dayymb'] || !$postdata['dayhour']){
		exi('请选择请假日期和课时！','error');
	}
	$data =array(
		'bodyid'=>intval($postdata['bodyid']),
		'storeid'=>intval($postdata['storeid']),
		'lesson_id'=>intval($postdata['lesson_id']),
		'classid'=>intval($postdata['classid']),
		'remark'=>$postdata['remark'],
		'dayymb'=>$postdata['dayymb'],
		'dayhour'=>$postdata['dayhour'],
		'logsn'=>get_ordersn('jy_kexie_body_out','logsn')
	);
	if($postdata['id']){
		pdo_update('jy_kexie_body_out',$data,array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功','','jy_kexie/body_out/main/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kexie_body_out',$data);
		exi('申请请假成功','','jy_kexie/body_out/main/');
	}
}
if($_RQ['op']=='lesson_time'){
	if($_RQ['id']===''){
		exi('请选择班级','error');
	}
	$dayid=intval($_RQ['day']);
	if(!isset($_RQ['day'])){
		exi('请重新选择日期','error');
	}
	$data=$this->model('lesson')->get_days($_RQ['id'],7);
	$return['data']=$data;
	$post=get_postdata();
	$class=pdo_get('jy_kexie_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$work_time=$class['work_time'];
	$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$dayid." day"))));
	if(strpos($work_time,';'.$weekid.';')!== false){
			$iswork=1;
	}
	$class_lesson=json_decode($class['class_lesson'],true);
	//获取当前时间
	$time=time();
	$lessons=array();
	if($class_lesson){
		foreach($class_lesson as $k=>&$cv){
			$lessons[$cv['xinqi']][]=$cv;
		}
		if($lessons[$weekid]){
				foreach($lessons[$weekid] as &$item){
					$item['dayid']=$dayid;
					$item['dayymb']=$data[$dayid]['ymd'];
					$item['dayhour']=$item['time']['start'];
					//时间转为时间戳
					$item['shijian']=strtotime($item['dayymb'].$item['dayhour']);
					if($item['shijian']<time()){
						$item['show']=0;
					}else{
						$item['show']=1;
					}
					$item['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lessonid']),'title');
				}
		}
		$lesson_time=$lessons[$weekid]?:array();
	}
	$return['lesson_time']=$lesson_time;
	$return['success']=1;
	$return['weekid']=$weekid;
	$return['dayid']=$dayid;
	exi($return);
}
if($_RQ['op']=='clenlset'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的假条！','error');
	}
	pdo_update('jy_kexie_body_out',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}